package com.cdacp.openapi.example;

import com.alibaba.fastjson.JSON;
import com.cdacp.openapi.common.Signature;
import com.cdacp.openapi.test.TestBase;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询用户免密支付接口是否打开接口
 */
public class WhetherOpenOneStepPaymentTest extends TestBase {

    String url = "https://openapi.cdacp.com";
    String appId = "20210113798975187478380544";
   String privateKey ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjM9pBabIsz6wGrbOShQuVyORKUwagN+IcKa21Eh4lix9QJR3Mz7oK2S+8/d0fYiPB8ffNFlgP+JI49anuMcjMnhcNdmE2BaJTabnXNuWIpAoo03NzdiaHXZ/SSkBMuF5rukg6c2F5dlSJeP2UoA0GWUb8cEfFoIyqh+RgCaCsh6zJ5oso393o9Vh7vNonMzXCWSsEv3vtuH4PDctOxfgKpAAexrPtLB0jbnkPpAR8GqZXwty0XM7MdcI8uT4dq++H/AwgTOzxIw0d3GCh7ewsGJM+Ba1yCDSxNhXJ+0gDMGK5jHwubuCq+ns24URHpYZOrvEDcOdajeq+GWV3CED7AgMBAAECggEBAJjQ1s9lXxciphvizkCmH8DzSQBnWc7wROLGDAd+nYfeC0YKWysKX1QFJfQ7abAXPxA9uCDriS37PT/kmGeXakRey4spi8D/6Y1ddEyW9LrLj0/HH1XADje2bQ6FxEuSX6dT2IoSu/T13haTmi5u54NIWXXK1QmyWcTcVfWvTin9pQ3Q4Qupft9Cdrk2RFGHFNpw4HbSXEsjU0BEITV4dv/GpDbl8Z1LevQ3tf6OFX9sTo4AXcx+co3AMm8p+CgTuOCq51qYgR0VgA3q5akYQUqIa2Tn1MZ3BvyWeJ8Zpkp7tjzZvhKedSnn2cfVxxY+ACwcQ+aiI4rKLWiOas4A8AECgYEA7/cfMnv9H3Ms2lJ2863pmdNWRd2vRbUSVopjGOEl4L0zwcSj7ha0/vJx6nrZTDMJ83gGUB5w5pILl+t/KX4BiUa2iP47uX3SRki2xlfQUhyV38s6m6ICUOE7q0MYSi7/LQR+KodqDhzWmtBKn28vywDH0BOIRUGNdQlJKBv8lPsCgYEArhud4E2X/CxFvEMqsclC6GT2fBgO+OqMWL7yqS1VJhgkde3lfzKQNrsCI0afVYS85/64UeUKcIZcg8cXl4O1G70Uc/vL8736V8SW8wV7uvBGLapDZjAfdvMW4xUMZvrfRDSo4nVqS6yyndBCXRNgVmYjd8vdejQ+ffRw2BcbRAECgYApn8whjbBSjD/zfupzSm+C503HwLRuewI7ULXv0t4nJaqcqedlnTq4HoX0Dmsy9Id9XAx8SUt8M0kfiRH9uIW7sjBngvIJMvIOjv5lWUYheCPnNfFsnaWZMfiPtIu6Enzg6i6YYfTbjdUL9aV8EmVP6Uq/pSO2H7DR5FJ0SqZ9kQKBgDz48O4HXm1yd615fC8sw2CtUuCccEndoe7GWCbE+GfR1pryCAfmhjiUfygpaE1DogBA5P3RDzne4/FGPM3T0w32xn3jtUYTxuMLtq0+w1jxIjKFyPq1aZ93fBFkYqaBvsSdCHPeGK6NuTOpJkSAL6GyPUOLwnE0cJodi5MxbFwBAoGBAIocnopzyiOjpjEjb/EmTRkMn/u/025XY0cWfXA6dQXOzYA9zjIS6T+FubaMvSp50ks4Jq6N4A/vMkBBZPrYXUliNyX28ZbJy8g084NTbLDOBg6aYGd6OSo9v9ZyvML9LiFyCzVkImxWLNBWqLuvObRXvdbTPU7NQWF8P0TGBlxa";
    /**
     参数	            类型	    是否必填	    最大长度	    描述	            示例值
     app_id	        String	是	        32	    cdacp分配给开发者的应用ID	2014072300007148
     method	        String	是	        128	    接口名称	trade.fastpay.refund.query
     format	        String	否	        40	    仅支持JSON	JSON
     charset	    String	是	        10	    请求使用的编码格式，如utf-8,gbk,gb2312等	utf-8
     sign_type	    String	是	        10	    商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2	RSA2
     sign	        String	是	        344	    商户请求参数的签名串，详见签名	详见示例
     timestamp	    String	是	        19	    发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"	2014-07-24 03:07:50
     version	        String	是	        3	    调用的接口版本，固定为：1.0	1.0
     app_auth_token	String	否	        40	    详见应用授权概述
     biz_content	    String	是		请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
     */
    // 这个请求会路由到服务
    @Test
    public void testGet() throws Exception {

        // 公共请求参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("app_id", appId);
        params.put("method", "onestep.whether.param.post");//方法名
        params.put("format", "json");
        params.put("charset", "utf-8");
        params.put("sign_type", "RSA2");
        params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        params.put("version", "1.0");

        // 业务参数
        Map<String, Object> bizContent = new HashMap<>();
        bizContent.put("userId", "315");
        bizContent.put("packageName", "com");
        bizContent.put("appId", appId);
        params.put("biz_content", JSON.toJSONString(bizContent));
        String content = Signature.getSignContent(params);
        String sign = Signature.rsa256Sign(content, privateKey, "utf-8");
        params.put("sign", sign);

        System.out.println("----------- 请求信息 -----------");
        System.out.println("请求参数：" + buildParamQuery(params));
        System.out.println("商户秘钥：" + privateKey);
        System.out.println("待签名内容：" + content);
        System.out.println("签名(sign)：" + sign);
        System.out.println("URL参数：" + buildUrlQuery(params));

        System.out.println("----------- 返回结果 -----------");
        String responseData = post(url, params);// 发送请求
        System.out.println(responseData);
    }




}
